package com.hmall.user.config;

import cn.hutool.core.util.StrUtil;
import com.hmall.user.domain.po.SysPermissions;
import com.hmall.user.service.SysPermissionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class ControllerMappingListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;
    @Resource
    private SysPermissionsService sysPermissionsService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 获取所有请求映射
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();

        // 打印每个请求映射的信息
        handlerMethods.forEach((info, method) -> {
            String mappings = info.getPatternsCondition().getPatterns().stream()
                .collect(Collectors.joining(", "));
            String methods = info.getMethodsCondition().getMethods().stream()
                .map(Enum::toString)
                .collect(Collectors.joining(", "));
            log.info("请求信息为 mappingInfo = {}",methods+"-"+mappings);
            try {
                if(!mappings.contains("error") && !mappings.contains("swagger")){
                    SysPermissions permissions = new SysPermissions();
                    permissions.setName(methods+"-"+mappings);
                    String mappingName = getMappingName(method);
                    if(StrUtil.isNotBlank(mappingName)){
                        permissions.setDescription(mappingName);
                    }
//                    sysPermissionsService.save(permissions);
                }
            } catch (Exception e) {
                log.error("保存权限异常",e);
            }
        });
    }


    private String getMappingName(HandlerMethod method) {
        if (method.getMethodAnnotation(GetMapping.class) != null && StrUtil.isNotBlank(method.getMethodAnnotation(GetMapping.class).name())) {
            return method.getMethodAnnotation(GetMapping.class).name();
        } else if (method.getMethodAnnotation(PostMapping.class) != null && StrUtil.isNotBlank(method.getMethodAnnotation(PostMapping.class).name())) {
            return method.getMethodAnnotation(PostMapping.class).name();
        } else if (method.getMethodAnnotation(PutMapping.class) != null && StrUtil.isNotBlank(method.getMethodAnnotation(PutMapping.class).name())) {
            return method.getMethodAnnotation(PutMapping.class).name();
        } else if (method.getMethodAnnotation(DeleteMapping.class) != null && StrUtil.isNotBlank(method.getMethodAnnotation(DeleteMapping.class).name())) {
            return method.getMethodAnnotation(DeleteMapping.class).name();
        } else if (method.getMethodAnnotation(PatchMapping.class) != null && StrUtil.isNotBlank(method.getMethodAnnotation(PatchMapping.class).name())) {
            return method.getMethodAnnotation(PatchMapping.class).name();
        } else if (method.getMethodAnnotation(RequestMapping.class) != null && StrUtil.isNotBlank(method.getMethodAnnotation(RequestMapping.class).name())) {
            return method.getMethodAnnotation(RequestMapping.class).name(); // Generic request mapping
        }
        return null;
    }
}